﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ElfTyping.States.GameState.Entities;
using Microsoft.Xna.Framework;
using ElfTyping.Animations;

namespace ElfTyping.States.GameState.Effects
{
    public class ArrowEffect : GameEffect
    {
        Enemy Target { get; set; }

        Vector2 _topLeftPosition;
        Vector2 TopLeftPosition
        {
            get { return _topLeftPosition; }
            set
            {
                _topLeftPosition = value;
                DrawBox.Offset((int)value.X - DrawBox.Left, (int)value.Y - DrawBox.Top);
            }
        }

        public ArrowEffect(Player player, Enemy target)
            : base()
        {
            Target = target;
            TimeLeft = 0.35f;
            DrawBox = new Rectangle(player.DrawBox.Left, player.DrawBox.Center.Y - 3, 19, 5);
            CurrentAnimation = new Animation(@"Textures\GameState\Arrow");

            MoveTo(new Vector2(player.DrawBox.Left, player.DrawBox.Center.Y - 3));
        }

        public override void Tick(float dt)
        {
            Vector2 speed = new Vector2(-Target.MoveSpeed + (Target.CenterPosition.X - TopLeftPosition.X) / TimeLeft,
                    (Target.CenterPosition.Y - 2 - TopLeftPosition.Y) / TimeLeft);

            MoveTo(TopLeftPosition + speed * dt);

            base.Tick(dt);
        }

        private void MoveTo(Vector2 position)
        {
            TopLeftPosition = position;

            Rectangle temp = DrawBox; // can't modify DrawBox directly, can only set/get
            temp.Offset((int)position.X - DrawBox.Left, (int)position.Y - DrawBox.Top);
            DrawBox = temp;
        }
    }
}
